package com.mytrip.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.quartz.Scheduler;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import com.mytrip.model.SysProp;

import freemarker.template.Template;

/**
 * 常量
 * 
 * @author Nicholas
 * @time Aug 19, 2008 1:29:13 PM
 */
public class Constant{

	public static Set<Scheduler> schedulerSet = new HashSet<Scheduler>();

	private static Map<String, Object> sysProperties;

	public static Map<String, Object> getSysProperties(){
		return sysProperties;
	}

	private static ResourceBundle commonConf;

	private static String _useModuleDomailUrl;

	private static String _useSessionInMemcache;

	static {
		commonConf = ResourceBundle.getBundle("common");
		_useModuleDomailUrl = commonConf.getString("useModuleDomailUrl");

		if ("1".equals(_useModuleDomailUrl)) {
			_useSessionInMemcache = "1";
		} else {
			_useSessionInMemcache = commonConf.getString("useSessionInMemcache");
		}
	}

	public static String contextRealPath;

	public static String UPLOAD_TEMPLATE_SAVE_PATH = "/var/upload/mailTemplate/";

	// public static String contextPath;

	public static final int COOKIE_EXPIRE_MINUTES = 3 * 60;

	public static final String TEMPLATE_PATH_MAIL = "mail";

	public static final String TEMPLATE_PATH_SMS = "sms";

	public static final String MAIL_SENDER_TEMPLATE = "email_sender.ftl";

	public static final String TEMPLATE_PATH_JASPER_REPORT = "template/jasper-report";

	public static final String DEFAULT_SUBJECT_TEMPLATE_NAME = "default_subject.ftl";

	public static final String DEFAULT_CONTENT_TEMPLATE_NAME = "default_content.ftl";

	public static final String DEFAULT_ENCODING = "UTF-8";

	public static final String comMailServerAccount = "sys@mytrip.com";

	public static final String EMAIL_SUBJECT_MEMORY_SIGN = "Email_Subject_";// email主题在内存中的标识符

	public static final String EMAIL_CONTENT_MEMORY_SIGN = "Email_Content_";// email内容在内存中的标识符

	public static Map<String, Template> EMAIL_MEMORY_TEMPLATE_MAP = new HashMap<String, Template>();// email在内存中的模板

	public static List<JavaMailSenderImpl> MAIL_SENDER_LIST = new ArrayList<JavaMailSenderImpl>(0);// 存储各邮件服务商的发送EDM配置信息

	public static void setSysPropListToMap(List<SysProp> sysPropList){
		HashMap<String, Object> map = new HashMap<String, Object>();
		for (Iterator<SysProp> iterator = sysPropList.iterator(); iterator.hasNext();) {
			SysProp sp = iterator.next();
			String type = sp.getType().toLowerCase();
			String valString = sp.getValue();
			Object value;
			if (type.equals("string")) {
				value = valString;
			} else if (type.equals("double")) {
				value = Double.valueOf(valString.trim());
			} else if (type.equals("long")) {
				value = Long.valueOf(valString.trim());
			} else if (type.equals("integer") || type.equals("int")) {
				value = Integer.valueOf(valString.trim());
			} else if (type.equals("map")) {
				value = JSONObject.toBean(JSONObject.fromObject(valString), Map.class);
			} else if (type.equals("list")) {
				value = JSONArray.toCollection(JSONArray.fromObject(valString), ArrayList.class);
			} else {
				Class<?> c;
				try {
					c = Class.forName(sp.getType());
				} catch (ClassNotFoundException e) {
					try {
						c = Class.forName("com.mytrip.model." + sp.getType());
					} catch (ClassNotFoundException e1) {
						try {
							c = Class.forName("com.mytrip.dto." + sp.getType());
						} catch (ClassNotFoundException e2) {
							throw new RuntimeException(e2);
						}
					}
				}
				value = JSONObject.toBean(JSONObject.fromObject(valString), c);
			}
			map.put(sp.getName(), value);
		}
		sysProperties = map;
	}

	public static String getCommonConf(String k){
		return commonConf.getString(k);
	}

	public static boolean useSessionInMemcache(){
		return "1".equals(_useSessionInMemcache);
	}

	public static boolean useModuleDomailUrl(){
		return "1".equals(_useModuleDomailUrl);
	}

}
